<?php
if (!defined('IN_IPB')) exit("<h1>Incorrect access</h1> You may not access this file without the acp.");

class statistics extends template_plugin
{
	var $ipsclass;
	
	var $name = "Statistics";
	var $multiple = 1;
	
	var $settings = array();
	
	function _load(&$plugin)
	{
		parent::_load($plugin);
		
		if($plugin['link'])
		{
			$this->settings = unserialize($plugin['plugin_config']);
		}
		else
		{
			$this->settings['count_richest'] = 5;
			$this->settings['count_most_inv_item'] = 5;
			$this->settings['count_most_item_bought'] = 5;
			$this->settings['count_most_item_used'] = 5;
			$this->settings['count_richest_bank'] = 5;
			$this->settings['count_richest_account'] = 5;
		}
	}
	
	function acp_settings()
	{
		$html = "";
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Show how many richest members?</b><br />If you put in 5, it will show the 5 richest members. Input 0 to disable or not to show.",
															$this->ipsclass->adskin->form_simple_input('count_richest',$this->settings['count_richest']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Show how many members who has the most items?</b><br />If you put in 5, it will show the top 5 members with the most items. Input 0 to disable or not to show.",
															$this->ipsclass->adskin->form_simple_input('count_most_inv_item',$this->settings['count_most_inv_item']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Show how many items most bought?</b><br />If you put in 5, it will show the top 5 items most bought. Input 0 to disable or not to show.",
															$this->ipsclass->adskin->form_simple_input('count_most_item_bought',$this->settings['count_most_item_bought']),
													)	);
		
		$html .= $this->ipsclass->adskin->add_td_row(array(
															"<b>Show how many items most used?</b><br />If you put in 5, it will show the top 5 items most used. Input 0 to disable or not to show.",
															$this->ipsclass->adskin->form_simple_input('count_most_item_used',$this->settings['count_most_item_used']),
													)	);
		
		if($this->ipsclass->market_class->count_plugin_filename("bank.php"))
		{
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<b>Show how many richest members in the bank?</b><br />If you put in 5, it will show the 5 richest member in the bank. Input 0 to disable or not to show.",
																$this->ipsclass->adskin->form_simple_input('count_richest_bank',$this->settings['count_richest_bank']),
														)	);
			
			$html .= $this->ipsclass->adskin->add_td_row(array(
																"<b>Show how many richest bank accounts?</b><br />If you put in 5, it will show the 5 richest bank accounts. Input 0 to disable or not to show.",
																$this->ipsclass->adskin->form_simple_input('count_richest_account',$this->settings['count_richest_account']),
														)	);
		}
		
		return $html;
	}
	
	function acp_save_settings(&$input)
	{
		$save = array();
		$save['count_richest'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_richest'],'int');
		$save['count_most_inv_item'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_most_inv_item'],'int');
		$save['count_most_item_bought'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_most_item_bought'],'int');
		$save['count_most_item_used'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_most_item_used'],'int');
		if($this->ipsclass->market_class->count_plugin_filename("bank.php"))
		{
			$save['count_richest_bank'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_richest_bank'],'int');
			$save['count_richest_account'] = $this->ipsclass->numbers->parse_input_number($this->ipsclass->input['count_richest_account'],'int');
		}
		
		$input['plugin_config'] = serialize($save);
	}
	
	function acp()
	{
		$this->ipsclass->admin->show_inframe($this->ipsclass->vars['board_url']."/index.".$this->ipsclass->vars['php_ext']."?autocom=market&plugin=".$this->plugin['link']);
	}
	
	function run()
	{
		$this->ipsclass->nav = array("<a href='{$this->ipsclass->market_class->base_url}plugin={$this->ipsclass->input['plugin']}'>".$this->plugin['name']."</a>");
		
		$this->ipsclass->load_template('skin_market_plugin');
		$this->ipsclass->load_language('lang_market_plugin');
		
		$output = array();
		
		if($this->settings['count_richest'])
		{
			$i = 0;
			$rows = array();
			$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'members','order'=>$this->ipsclass->vars['market_members_field'].' DESC','limit'=>array($this->settings['count_richest'])));
			$this->ipsclass->DB->exec_query();
			while($info = $this->ipsclass->DB->fetch_row())
			{
				$info['members_display_name'] = $this->ipsclass->market_class->format_name($info['id'], $info['members_display_name'], $info['mgroup']);
				$rows[] = array('rank'=>++$i,'name'=>$info['members_display_name'],'number'=>$this->ipsclass->numbers->format_currency($info[$this->ipsclass->vars['market_members_field']]));
			}
			
			$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_richest',$rows);
		}
		if($this->settings['count_most_inv_item'])
		{
			$i = 0;
			$rows = array();
			$this->ipsclass->DB->build_query(array(
													'select' => 'COUNT(*) as no_item',
													'from' => array('market_inventory'=>'i'),
													'add_join' => array(
																			array(
																					'select' => 'm.members_display_name, m.mgroup',
																					'from' => array('members'=>'m'),
																					'where' => 'm.id=i.member_id',
																					'type' => 'inner',
																				),
																		),
													'group' => 'i.member_id',
													'order' => 'no_item DESC',
													'limit' => array($this->settings['count_most_inv_item']),
											)	);
			$this->ipsclass->DB->exec_query();
			while($info = $this->ipsclass->DB->fetch_row())
			{
				$info['members_display_name'] = $this->ipsclass->market_class->format_name($info['id'], $info['members_display_name'], $info['mgroup']);
				$rows[] = array('rank'=>++$i,'name'=>$info['members_display_name'],'number'=>$this->ipsclass->do_number_format($info['no_item']));
			}
			
			$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_most_inv_item',$rows);
		}
		if($this->settings['count_most_item_bought'])
		{
			$i = 0;
			$rows = array();
			$this->ipsclass->DB->build_query(array('select'=>'item_id,item_name,SUM(quanity) as sum_bought','from'=>'market_buy_logs','where'=>"method='buy'",'group'=>'item_id','order'=>'sum_bought DESC','limit'=>array($this->settings['count_most_item_bought'])));
			$this->ipsclass->DB->exec_query();
			while($info = $this->ipsclass->DB->fetch_row())
			{
				$rows[] = array('rank'=>++$i,'name'=>is_array($this->ipsclass->cache['market_items'][$info['item_id']]) ? $this->ipsclass->cache['market_items'][$info['item_id']]['item_name'] : $info['item_name'],'number'=>$this->ipsclass->do_number_format($info['sum_bought']));
			}
			
			$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_most_inv_bought',$rows);
		}
		if($this->settings['count_most_item_used'])
		{
			$i = 0;
			$rows = array();
			$this->ipsclass->DB->build_query(array('select'=>'COUNT(*) as count_items,item_id','from'=>'market_item_logs','group'=>'item_id','order'=>'count_items DESC','limit'=>array($this->settings['count_most_item_used'])));
			$this->ipsclass->DB->exec_query();
			while($info = $this->ipsclass->DB->fetch_row())
			{
				$rows[] = array('rank'=>++$i,'name'=>$this->ipsclass->cache['market_items'][$info['item_id']]['item_name'],'number'=>$this->ipsclass->do_number_format($info['count_items']));
			}
			
			$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_most_inv_used',$rows);
		}
		
		if(is_array($this->ipsclass->cache['market_plugin']) && count($this->ipsclass->cache['market_plugin']))
		{
			foreach($this->ipsclass->cache['market_plugin'] as $plugin)
			{
				if($plugin['filename'] == "bank.php")
				{
					$bank_info = $plugin;
				}
			}
		}
		if(is_array($bank_info) && in_array($this->ipsclass->member['mgroup'],$bank_info['view_permission']))
		{
			if($this->settings['count_richest_bank'])
			{
				$i = 0;
				$rows = array();
				$this->ipsclass->DB->build_query(array(
														'select' => 'SUM(a.balance) as total_balance',
														'from' => array('market_bank_accounts'=>'a'),
														'add_join' => array(
																				array(
																						'select' => 'm.members_display_name, m.mgroup',
																						'from' => array('members'=>'m'),
																						'where' => 'id=a.a_mem_id',
																						'type' => 'inner',
																					),
																			),
														'group' => 'a.a_mem_id',
														'order' => 'total_balance DESC',
														'limit' => array($this->settings['count_richest_bank']),
												)	);
				$this->ipsclass->DB->exec_query();
				while($info = $this->ipsclass->DB->fetch_row())
				{
					$info['members_display_name'] = $this->ipsclass->market_class->format_name($info['id'], $info['members_display_name'], $info['mgroup']);
					$rows[] = array('rank'=>++$i,'name'=>$info['members_display_name'],'number'=>$this->ipsclass->numbers->format_currency($info['total_balance']));
				}
				
				$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_richest_member_in_bank',$rows);
			}
			if($this->settings['count_richest_account'])
			{
				$bank = $this->ipsclass->market_class->load_plugin($bank_info);
				$i = 0;
				$rows = array();
				$this->ipsclass->DB->build_query(array('select'=>'*','from'=>'market_bank_accounts','order'=>'balance DESC','limit'=>array($this->settings['count_richest_account'])));
				$this->ipsclass->DB->exec_query();
				while($info = $this->ipsclass->DB->fetch_row())
				{
					$rows[] = array('rank'=>++$i,'name'=>$bank->_build_bank_account_id($info['account_id'],$info['a_random'],$info['a_mem_id']),'number'=>$this->ipsclass->numbers->format_currency($info['balance']));
				}
				
				$output[] = $this->ipsclass->compiled_templates['skin_market_plugin']->stats_table('stats_richest_account_in_bank',$rows);
			}
		}
		$this->ipsclass->output .= $this->ipsclass->compiled_templates['skin_market_plugin']->stats_header().implode("\n<br />\n",$output);
	}
}
?>